#!/bin/bash

# 设置脚本在发生错误时停止执行
set -e

# 输出日志函数
log() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - INFO - $1"
}

# 错误处理函数
error() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - ERROR - $1"
    exit 1
}

# 提示用户输入 kubeadm join 的主体命令
read -p "请输入 kubeadm join 的主体命令（例如：kubeadm join 10.8.166.240:6443 --token uq6h3m.jgrn7qffjk4e7ihi --discovery-token-ca-cert-hash sha256:ce882c56d3f55023c9859e5cdf80cab130d6613d91ab5e395d8d85506c198ccb）: " kubeadm_join_command
if [ -z "$kubeadm_join_command" ]; then
    error "kubeadm join 命令不能为空。"
fi

# 执行 kubeadm join 命令并添加 --cri-socket 参数
log "Running kubeadm join command with --cri-socket parameter..."
sudo sh -c "$kubeadm_join_command --cri-socket unix:///var/run/cri-dockerd.sock"
if [ $? -ne 0 ]; then
    error "Failed to run kubeadm join command."
fi
log "kubeadm join command executed successfully."

log "All tasks completed successfully."